package main

import (
	"fmt"
	"sort"
)

func main() {
	fmt.Println(merge([][]int{{1, 4}, {2, 3}}))
}

// {1,4} {2,3}
func merge(intervals [][]int) (ans [][]int) {
	sort.Slice(intervals, func(i, j int) bool { return intervals[i][0] < intervals[j][0] }) // 按照左端点从小到大排序
	for _, interval := range intervals {
		if len(ans) > 0 && ans[len(ans)-1][1] >= interval[0] {
			ans[len(ans)-1][1] = max(ans[len(ans)-1][1], interval[1])
		} else {
			ans = append(ans, interval)
		}
	}
	return
}
